package org.flower.isme.flower.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.flower.isme.common.preview.Preview;
import org.flower.isme.common.response.Page;
import org.flower.isme.common.response.R;
import org.flower.isme.flower.domain.dto.SoilDataDto;
import org.flower.isme.flower.domain.entity.SoilData;
import org.flower.isme.flower.domain.request.SoilDataPageRequest;
import org.flower.isme.flower.service.SoilDataService;
import org.flower.isme.netty.enums.Cmd;
import org.flower.isme.netty.handler.NettyServerHandler;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/soil")
@RequiredArgsConstructor
@Tag(name = "土壤")
public class SoilDataController {
    private final SoilDataService soilDataService;
    private final NettyServerHandler nettyServerHandler;

    /**
     * 上传土壤温度数据
     *
     * @param soilData 光照数据
     * @return R
     */
    @PostMapping("/insert")
    @Preview
    @Operation(summary = "上传土壤温度数据")
    public R<Void> updateLightData(@RequestBody SoilData soilData) {
        soilDataService.save(soilData);
        nettyServerHandler.send(Cmd.CMD_004, "123456", soilData);
        return R.ok();
    }

    /**
     * 分页查询土壤温度列表
     *
     * @param request 查询信息
     * @return R
     */
    @GetMapping("/list")
    @Preview
    @Operation(summary = "分页查询土壤温度列表")
    public R<Page<SoilDataDto>> list(SoilDataPageRequest request) {
        return R.ok(soilDataService.queryPage(request));
    }
}
